import './index.less'
import {getCompanyName, saveInformation} from "../../utils/network/network.config.js";
import '../../utils/network/city/city.js'
import {animate} from "../../utils/network/city/animate";
import {getElement} from '../../utils/network/networkutils'

let companyname=getElement('companyname');
let inputlist=getElement('inputlist');
let addrshow02=getElement('addr-show02');
let textbox=getElement('textbox');
let name=getElement('name');
let phone=getElement('phone');
let mail=getElement('mail');
let close=getElement('close');
let checktoast=getElement('checktoast');
let noempnametoast=getElement('noempnametoast');
let noempaddrtoast=getElement('noempaddrtoast');
let noemptytoast=getElement('noemptytoast');
let phonetoast=getElement('phonetoast');
let empphonetoast=getElement('empphonetoast');
let mailtoast=getElement('mailtoast');
let empmailtoast=getElement('empmailtoast');
let checkbox=getElement('checkbox');
let submit=getElement('submit');
let serverprotocol=getElement('serverprotocol');
let cancel=getElement('cancel');
let protocol=getElement('protocol');
let useprotocol=getElement('use-protocol');
let useprotocolA=getElement('useprotocol');
let useclose=getElement('useclose');
let xyclose=getElement('xyclose');
let mailReg = /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/;
let phoneReg=/^1[3|4|5|7|8|9][0-9]{9}$/;
let TrimStrReg=/^\s+|\s+$/g;
let flag = true;

xyclose.onclick=function () {
    protocol.style.cssText="display: none;";
};
useclose.onclick=function () {
    useprotocol.style.cssText="display: none;";
};

serverprotocol.onclick=function () {
    protocol.style.cssText="display: block;";
        animate({
            ele: protocol,
            target: {
            opacity: 1,
                },
            effect: ['Linear'],
            duration: 300,
        });
};
useprotocolA.onclick=function () {
    useprotocol.style.cssText="display: block;";
    animate({
        ele: useprotocol,
        target: {
            opacity: 1,
        },
        effect: ['Linear'],
        duration: 300,
    });
}

companyname.onkeyup=function () {
    let value=companyname.value;
    value=value.replace(TrimStrReg, "");
    if (value){
        getCompanyNameData({'keyword':value,'isAjax':1,ieStamp:new Date().getTime()})
    }else {
        inputlist.style.cssText="display: none;";
    }

};
function getCompanyNameData(value){
    getCompanyName(value).then((res)=>{
        if(res.data.resultCode !== 'busi_ex'){
            bindListData(res.data.result)
        }
    })
}
function bindListData(data) {
    if (data){
        inputlist.style.cssText="display: block;";
        let str='';
        for (let i = 0; i < data.length; i++) {
            let item = data[i];
            str+=`<li>${item.name}</li>`
        }
        inputlist.innerHTML=str
    }
}
inputlist.onclick=function (e) {
    e = e || window.event;
    let target = e.target || e.srcElement;
    let value;
    if (target.tagName==='LI'){
        value=target.innerHTML;
    }
    companyname.value=value;
    inputlist.style.cssText="display: none;";
};

//焦点操作
companyname.onfocus=function () {
    onFocusChange(companyname,noempnametoast)
};
addrshow02.onfocus=function () {
    onFocusChange(addrshow02,noempaddrtoast)
};
textbox.onfocus=function () {
    textbox.style.borderColor='#83A2FF';
};
name.onfocus=function () {
    onFocusChange(name,noemptytoast)
};
phone.onfocus=function () {
    onFocusChange(phone,empphonetoast)
};
mail.onfocus=function () {
    onFocusChange(mail,empmailtoast)
};

//失焦点操作
companyname.onblur=function () {
    companyAndNameBlur(companyname,noempnametoast)
};
addrshow02.onblur=function () {
    companyAndNameBlur(addrshow02,noempaddrtoast)
};
name.onblur=function () {
    companyAndNameBlur(name,noemptytoast)
};
textbox.onblur=function () {
    textbox.style.borderColor='#D2D2D2';
};
phone.onblur=function () {
    phoneAndMailBlur(phone,phonetoast,phoneReg)
};
mail.onblur=function () {
    phoneAndMailBlur(mail,mailtoast,mailReg)
};

//提交操作

submit.onclick = function () {
    let companynamevalue = companyname.value;
    let cityvalue = addrshow02.value;
    let textboxvalue = textbox.value;
    let namevalue = name.value;
    let phonevalue = phone.value;
    let mailvalue = mail.value;
    let number = 0;
    let companynamevalueR = companyname.value.replace(TrimStrReg, "");
    let namevalueR = namevalue.replace(TrimStrReg, "");
    let phonevalueR = phone.value.replace(TrimStrReg, "");
    let mailvalueR = mail.value.replace(TrimStrReg, "");
    let information = {
        tenantname: '',
        province: '',
        city: '',
        county: '',
        detailaddress: '',
        linkman: '',
        mobilephone: '',
        email: ''
    };
    if (companynamevalue === '') {
        noempnametoast.style.cssText = "display: block;";
        companyname.style.borderColor = 'red'
    } else if (companynamevalueR === '') {
        noempnametoast.style.cssText = "display: block;";
        companyname.style.borderColor = 'red'
    } else {
        noempnametoast.style.cssText = "display: none;";
        number = number + 1;
        information.tenantname = companynamevalue
    }
    if (cityvalue === '') {
        noempaddrtoast.style.cssText = "display: block;";
        addrshow02.style.borderColor = 'red'
    } else {
        noempaddrtoast.style.cssText = "display: none;";
        number = number + 1;
        addrshow02.style.borderColor = '#D2D2D2';
        let cityarr = cityvalue.split('/');
        information.province = cityarr[0] || '';
        information.city = cityarr[1] || '';
        information.county = cityarr[2] || ''
    }
    information.detailaddress = textboxvalue;
    if (namevalue === '') {
        noemptytoast.style.cssText = "display: block;";
        name.style.borderColor = 'red'
    } else if (namevalueR === '') {
        noemptytoast.style.cssText = "display: block;";
        name.style.borderColor = 'red'
    } else {
        noemptytoast.style.cssText = "display: none;";
        number = number + 1;
        information.linkman = namevalue
    }
    if (phonevalue === '') {
        empphonetoast.style.cssText = "display: block;";
        phone.style.borderColor = 'red'
    } else if (!phoneReg.test(phonevalueR)) {
        phonetoast.style.cssText = "display: block;";
        phone.style.borderColor = 'red'
    } else {
        empphonetoast.style.cssText = "display: none;";
        number = number + 1;
        information.mobilephone = phonevalue
    }
    if (mailvalue === '') {
        empmailtoast.style.cssText = "display: block;";
        mail.style.borderColor = 'red';
    } else if (!mailReg.test(mailvalueR)) {
        mailtoast.style.cssText = "display: block;";
    } else {
        empmailtoast.style.cssText = "display: none;";
        number = number + 1;
        information.email = mailvalue
    }
    if (number === 5) {
        if (checkbox.checked) {
            checktoast.style.cssText = "display: none;";
            if (flag) {
                flag = false;
                getSaveInformationData({...information, 'isAjax': 1});
            }

        } else {
            checktoast.style.cssText = "display: block;";
            animate({
                ele: checktoast,
                target: {
                    opacity: 1
                },
                effect: ['Linear'],
                duration: 300,
            });
        }
    }
};




cancel.onclick=function () {
    window.location.href='/official-index.html';
};
close.onclick=function () {
    checktoast.style.cssText="display: none;";
};

function getSaveInformationData(obj) {
    saveInformation(obj).then((res)=>{
        if(res.data.resultCode !== 'busi_ex'){
            window.location.href='/official-jmppage.html';
        }
    })
}
function companyAndNameBlur(ele,eletoast) {
    let value=ele.value;
    let regValue=value.replace(TrimStrReg, "");
    if (value===''){
        ele.style.borderColor='#D2D2D2';
    }else if(regValue===''){
        eletoast.style.cssText="display: block;";
        ele.style.borderColor='red';
    }else{
        eletoast.style.cssText="display: none;";
        ele.style.borderColor='#D2D2D2';
    }
}
function phoneAndMailBlur(ele,eletoast,reg) {
    let value=ele.value;
    let regValue=value.replace(TrimStrReg, "");
    if (value===''){
        ele.style.borderColor='#D2D2D2';
        eletoast.style.cssText="display: none;"
    }else if(regValue===''){
        ele.style.borderColor='red';
        eletoast.style.cssText="display: block;"
    }else if(!reg.test(regValue)){
        ele.style.borderColor='red';
        eletoast.style.cssText="display: block;"
    }else {
        ele.style.borderColor='#D2D2D2';
        eletoast.style.cssText="display: none;"
    }
}
function onFocusChange(ele,eletoast) {
    ele.style.borderColor='#83A2FF';
    eletoast.style.cssText="display: none;";
}